//
//  messageViewController.m
//  giuseppe
//
//  Created by Apple on 15/7/13.
//  Copyright (c) 2015年 GiantUmbrella. All rights reserved.
//

#import "messageViewController.h"
#import "define.h"
#import "messageModel.h"

@interface messageViewController ()<UITableViewDataSource,UITableViewDelegate>{
    UITableView *_messageTableView;
    
    NSMutableArray *messageModelArray;
}

@end

@implementation messageViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    self.edgesForExtendedLayout = UIRectEdgeNone;
    
    
    
    //自定义的导航栏
    UIView *navView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, 64)];
    navView.backgroundColor = [UIColor whiteColor];
    navView.alpha = 0.90;
    
    UIButton *backButton = [UIButton buttonWithType:UIButtonTypeCustom];
    backButton.frame = CGRectMake(10, 33, 27, 16);
    [backButton setBackgroundImage:[UIImage imageNamed:@"back"] forState:UIControlStateNormal];
    [backButton addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside];
    
    UILabel *titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(kScreenWidth/2-50, 20, 100, 40)];
    titleLabel.text = @"消息";
    titleLabel.textAlignment = NSTextAlignmentCenter;
    
    UIView * view = [[UIView alloc]initWithFrame:CGRectMake(0, 64, kScreenWidth, 1)];
    view.backgroundColor = UIColorWithRGBA(229, 229, 231, 1);
    [self.view addSubview:view];
    
    
    [self _initView];

    [navView addSubview:backButton];
    [navView addSubview:titleLabel];
    
    
    [self.view addSubview:navView];
    
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
}


- (void)loadData{
    
    NSString *userId = @"1";
    NSDictionary *paras = @{@"userId":userId};
    
    
    //调用请求
    [[myDataAccess shardWebUtil] gettingMessageWithURLString:kNetPath_Code_gotPopMessage andParas:paras andTransferGottenData:^(id obj, NSError *error)  {
        
        NSLog(@"%@",obj);
        
        if (obj) {
            
            NSArray *dataArray = [obj objectForKey:@"dataList"];
            
            messageModelArray = [[NSMutableArray alloc]init];
            
            for (NSDictionary *dic in dataArray) {
                
                messageModel *model = [messageModel initWithDicAttributes:dic];
                [messageModelArray addObject:model];

            }
            
            [_messageTableView.legendHeader endRefreshing];
            [_messageTableView reloadData];
            
            
        }else{
            
        }

    }];

}

- (void)_initView{
    
    
    _messageTableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 65, kScreenWidth, kScreenHeight-64)];
    _messageTableView.delegate = self;
    _messageTableView.dataSource = self;
    _messageTableView.rowHeight = 70;
//    _messageTableView.separatorStyle = UITableViewCellEditingStyleNone;
    [self.view addSubview:_messageTableView];
    
    __weak __typeof(self) weakSelf = self;
    
    [_messageTableView  addLegendHeaderWithRefreshingBlock:^{
        
        [weakSelf loadData];
        
    }];
    
    [_messageTableView.legendHeader beginRefreshing];

}

#pragma mark - UITableView Delegate && DataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return messageModelArray.count;

}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    static NSString *ID = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    if (cell==nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:ID];
        
        
        
        //        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        
        
        messageModel *model = messageModelArray[indexPath.row];

        UILabel *nameLable = [[UILabel alloc]initWithFrame:CGRectMake(90, 7, 200, 30)];
        nameLable.text=[NSString stringWithFormat:@"%@",model.messageContent];
        nameLable.textAlignment = NSTextAlignmentLeft;
        nameLable.font = [UIFont systemFontOfSize:16];
        [cell addSubview:nameLable];
        
        UILabel *timeLable = [[UILabel alloc]initWithFrame:CGRectMake(nameLable.frame.origin.x, nameLable.frame.origin.y+nameLable.frame.size.height-2, 200, 30)];
        timeLable.text=model.messageDate;
        timeLable.textAlignment = NSTextAlignmentLeft;
        timeLable.font = [UIFont systemFontOfSize:16];
        [cell addSubview:timeLable];
        
        
        UIImageView * image = [[UIImageView alloc]initWithFrame:CGRectMake(10, 5, 60, 60)];
        [image sd_setImageWithURL:[NSURL URLWithString:model.messageImageURL] placeholderImage:[UIImage imageNamed:@"1"]];
        image.layer.masksToBounds = YES;
        image.layer.cornerRadius = 30.0;

        [cell addSubview:image];

//        UIView * view = [[UIView alloc]initWithFrame:CGRectMake(0, _messageTableView.rowHeight-1, kScreenWidth, 1)];
//        view.backgroundColor = UIColorWithRGBA(229, 229, 231, 1);
//        [cell addSubview:view];
//        
        
        
    }
    

    return cell;
    
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    return 70;
}


- (void)back{
    
    [self.navigationController popViewControllerAnimated:YES];
    
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    
    
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



@end
